package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SuperGroupActivity;
import com.ruoyi.system.domain.SuperProduct;
import com.ruoyi.system.service.ISuperGroupActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 拼团活动Controller
 * 
 * @author xjf
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/activity/activitygroup")
public class SuperGroupActivityController extends BaseController
{
    @Autowired
    private ISuperGroupActivityService superGroupActivityService;

    /**
     * 查询拼团活动列表
     */
    @PreAuthorize("@ss.hasPermi('activity:activitygroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuperGroupActivity superGroupActivity
    ,@RequestParam(required = false) Long fatherCategoryId)
    {
        if (fatherCategoryId != null) {
            if (superGroupActivity.getProduct() == null) {
                superGroupActivity.setProduct(new SuperProduct());  // 初始化 product 对象，避免空指针
            }
            superGroupActivity.getProduct().setFatherCategoryId(fatherCategoryId);  // 设置分类ID到 product 中
        }


        startPage();
        List<SuperGroupActivity> list = superGroupActivityService.selectSuperGroupActivityList(superGroupActivity);
        return getDataTable(list);
    }

    /**
     * 导出拼团活动列表
     */
    @PreAuthorize("@ss.hasPermi('activity:activitygroup:export')")
    @Log(title = "拼团活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuperGroupActivity superGroupActivity)
    {
        List<SuperGroupActivity> list = superGroupActivityService.selectSuperGroupActivityList(superGroupActivity);
        ExcelUtil<SuperGroupActivity> util = new ExcelUtil<SuperGroupActivity>(SuperGroupActivity.class);
        util.exportExcel(response, list, "拼团活动数据");
    }

    /**
     * 获取拼团活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:activitygroup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(superGroupActivityService.selectSuperGroupActivityById(id));
    }

    /**
     * 新增拼团活动
     */
    @PreAuthorize("@ss.hasPermi('activity:activitygroup:add')")
    @Log(title = "拼团活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuperGroupActivity superGroupActivity)
    {
        return toAjax(superGroupActivityService.insertSuperGroupActivity(superGroupActivity));
    }

    /**
     * 修改拼团活动
     */
    @PreAuthorize("@ss.hasPermi('activity:activitygroup:edit')")
    @Log(title = "拼团活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuperGroupActivity superGroupActivity)
    {
        return toAjax(superGroupActivityService.updateSuperGroupActivity(superGroupActivity));
    }

    /**
     * 删除拼团活动
     */
    @PreAuthorize("@ss.hasPermi('activity:activitygroup:remove')")
    @Log(title = "拼团活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(superGroupActivityService.deleteSuperGroupActivityByIds(ids));
    }
}
